package com.zhang.zhangapp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.HttpConfig;
import com.zhang.zhangapp.bean.shop.Shop;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2018/3/26.
 */

public class ShopAdapter extends BaseAdapter {

    private List<Shop> shops;
    private Context context;
    ImageOptions imageOptions = new ImageOptions.Builder()
            .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
            .setRadius(DensityUtil.dip2px(2))
            .setIgnoreGif(false)
            .setCrop(true)//是否对图片进行裁剪
            .setFailureDrawableId(R.mipmap.test)
            .setLoadingDrawableId(R.mipmap.test)
            .build();

    public ShopAdapter(List<Shop> shops, Context context) {
        this.shops = shops;
        this.context = context;
    }

    @Override
    public int getCount() {
        return shops == null ? 0 : shops.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_shop, null);
            x.view().inject(viewHolder, view);
            view.setTag(viewHolder);
        } else viewHolder = (ViewHolder) view.getTag();
        Shop shop = shops.get(i);
        viewHolder.shopDes.setText(shop.getAddress());
        if (shop.getRanges() == null || shop.getRanges().equals("")) {
            viewHolder.shopDistance.setVisibility(View.GONE);
        } else {
            viewHolder.shopDistance.setVisibility(View.VISIBLE);
            BigDecimal dis = new BigDecimal(shop.getRanges());
            if(dis.doubleValue()>1000){
                String disStr = dis.divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP).toString();
                viewHolder.shopDistance.setText(disStr+"KM");
            }else{
                viewHolder.shopDistance.setText(shop.getRanges() + "米");
            }
        }
        viewHolder.shopName.setText(shop.getShopName());
        x.image().bind(viewHolder.shopImage, HttpConfig.IMAGE_HTTP_PATH + shop.getListpic(), imageOptions);//
        viewHolder.shopPrice.setText("￥" + shop.getPerconsume()+"/人" + "  已售" + shop.getConsumeconCount());
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.shop_image)
        ImageView shopImage;
        @ViewInject(R.id.shop_name)
        TextView shopName;
        @ViewInject(R.id.shop_distance)
        TextView shopDistance;
        @ViewInject(R.id.shop_des)
        TextView shopDes;
        @ViewInject(R.id.shop_price)
        TextView shopPrice;


    }
}
